﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StuLib;

namespace 实验4_集合类型使用
{
    public partial class Form1 : Form
    {
        List<Student> list = null;
        Dictionary<string, Student> dicStus = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void init_List_Click(object sender, EventArgs e)
        {
            list = new List<Student>();
            list.Add(new Student("张三", "1001", 18));
            list.Add(new Student("李四", "1002", 19));
            list.Add(new Student("王五", "1003", 22));
            list.Add(new Student("赵六", "1004", 17));
            list.Add(new Student("张三丰", "1005", 16));
            list.Add(new Student("乔峰", "1006", 25));
        }

        private void show_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            foreach (Student stu in list)
            {
                // string fmt = "{0},{1},{2}岁";
                this.listBox1.Items.Add(stu);
            }
        }

        private void loop_sayHi_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            foreach (Student stu in list)
            {
                this.listBox1.Items.Add(stu.SayHi);
            }
        }

        private void count_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("目前共有" + list[0].getCount().ToString() + "位学生");
            MessageBox.Show("目前共有" + Student.getCount().ToString() + "位学生");
        }

        private void select_sno_Click(object sender, EventArgs e)
        {
            string sno = this.tb_in_sno.Text;
            this.listBox1.Items.Clear();
            foreach (Student stu in list)
            {
                if (stu.Sno == sno)
                {
                    //string fmt = "{0},{1},{2}岁";
                    //this.listBox1.Items.Add(string.Format(fmt, stu.SName, stu.Sno, stu.Age));
                    showAppendStu(stu, false);
                    return;
                }
            }
            this.listBox1.Items.Add("查无此人");
        }
        private void delete_sno_Click(object sender, EventArgs e)
        {
            string sno = this.tb_in_sno.Text;
            if (ConfirmDialog("确认删除"+ sno +"学生？", "确认框"))
            {
                foreach (Student stu in list)
                {
                    if (stu.Sno == sno)
                    {
                        list.Remove(stu);
                        MessageBox.Show("成功删除学号为" + sno + "的学生");
                        this.listBox1.Items.Remove(stu);
                        // 删除后刷新显示学生信息
                        show_Click(sender, e);
                        return;
                    }
                }
                MessageBox.Show("删除失败，没有学号为" + sno + "的学生");
            }
        }
        // 确认对话框
        bool ConfirmDialog(string msg, string title)
        {
            bool confirm = false;
            DialogResult dr = MessageBox.Show(msg, title, MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)//如果点击“确定”按钮
            {
                confirm = true;
            }
            return confirm;
        }

        private void delete_All_Click(object sender, EventArgs e)
        {
            if (ConfirmDialog("确认删除所有学生？", "确认框"))
            {
                list.Clear();
                // 重新点击show按钮，更新显示信息
                show_Click(sender, e);
            }
        }


        bool flag = false;
        int i = -1;
        private void random_Click(object sender, EventArgs e)
        {
            if (list.Count > 0)
            {
                flag = !flag;
                if (!flag && i != -1)
                {
                    showAppendStu(list[i], false);
                }
            }
            else
            {
                this.tb_out_random.Text = "没有学生可以选择，请先添加学生信息！";
            }
        }
        // 定时器事件
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (flag && list.Count > 0)
            {
                Random ra = new Random();
                i = ra.Next() % list.Count;
                tb_out_random.Text = list[i].SName;
                this.random.Text = "就你了！";
            }
            else
            {
                this.random.Text = "天选之子";
            }
        }

        private void showAppendStu(Student stu, bool append)
        {
            if (!append)
            {   // 清空原有内容
                this.listBox1.Items.Clear();
            }
            string fmt = "{0},{1},{2}岁";
            this.listBox1.Items.Add(string.Format(fmt, stu.SName, stu.Sno, stu.Age));
        }



    }
}
